package com.atguigu.gulimall.order.interceptor;

import com.atguigu.common.vo.MemberRsepVo;
import com.atguigu.gulimall.order.contant.CartConstant;
import com.atguigu.gulimall.order.vo.UserInfoTo;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

/**
 * <p>Title: LoginUserInterceptor</p>
 * Description：
 * date：2020/6/29 22:40
 */
@Component
public class LoginUserInterceptor implements HandlerInterceptor {
	public static ThreadLocal<MemberRsepVo> threadLocal=new ThreadLocal<>();

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		String url=request.getRequestURI();
		//远程获取订单接口放行
		Boolean match=new AntPathMatcher().match("/order/omsorder/getOrderById/**",url);
		if(match){
			return true;
		}
		MemberRsepVo loginUser = (MemberRsepVo) request.getSession().getAttribute("loginUser");
		HttpSession session = request.getSession();
		if(loginUser!=null){
			threadLocal.set(loginUser);
			return true;
		}else{
			session.setAttribute("msg", "未登录");
			response.sendRedirect("http://auth.gulimall.com/login.html");
			return false;
		}
	}
	
}
